package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.common.worker.client.exception.RestClientException;
import com.cloudlead.commonData.service.BaseDriverService;
import com.cloudlead.commonData.service.BaseFleetService;
import com.cloudlead.commonData.vo.BaseCompany;
import com.cloudlead.commonData.vo.BaseDriver;
import com.cloudlead.commonData.vo.BaseFleet;
import com.cloudlead.fleetAction.service.BaseDriverEditSer;
import com.cloudlead.fleetService.vo.BaseDriverVO;
import com.cloudlead.fleetService.vo.BaseFleetVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 123 on 2015/9/1.
 */
@Service
public class BaseDriverEditSerImpl implements BaseDriverEditSer {

    @Autowired
    BaseDriverService baseDriverService;
    @Autowired
    BaseFleetService baseFleetService;

    @Override
    //增加
    public void addBaseDriver(BaseDriverVO baseDriverVO) {
        BaseDriverVO qryVo=new BaseDriverVO();
        qryVo.setBaseCompany(baseDriverVO.getBaseCompany());
        qryVo.setMobile(baseDriverVO.getMobile());
        Page p=findAllBaseDriver(qryVo, 1, 1);
        if (p.getContent().size()>0){
            if(((BaseDriver)p.getContent().get(0)).getFlag().equals("1"))
            throw new RestClientException("手机号：\""+baseDriverVO.getMobile()+"\"已存在。该驾驶员停用状态，请恢复");
            else  throw new RestClientException("手机号：\""+baseDriverVO.getMobile()+"\"已存在。");
        }
        BaseDriver bd=new BaseDriver();
        BeanUtils.copyProperties(baseDriverVO, bd);
        if(baseDriverVO.getBaseFleetId()!=null&&!baseDriverVO.getBaseFleetId().equals("")){//
            BaseFleet bf=new BaseFleet();
            bf.setId(baseDriverVO.getBaseFleetId());
            bd.setBaseFleet(bf);
        }
        if(baseDriverVO.getBaseCompany()!=null&&!baseDriverVO.getBaseCompany().equals("")){//如果公司不为空
            BaseCompany bcy = new BaseCompany();
            bcy.setId(baseDriverVO.getBaseCompany());
            bd.setBaseCompany(bcy);
        }
        bd.setId(baseDriverVO.getId());
        bd.setName(baseDriverVO.getName());
        bd.setMobile(baseDriverVO.getMobile());
        bd.setIdentityCard(baseDriverVO.getIdentityCard());
        bd.setDrivingLicence(baseDriverVO.getDrivingLicence());
        bd.setDrivingType(baseDriverVO.getDrivingType());
        bd.setLimitedDate(baseDriverVO.getLimitedDate());
        bd.setQualificationNumber(baseDriverVO.getQualificationNumber());
        bd.setCompany(baseDriverVO.getCompany());
        bd.setFleetId(baseDriverVO.getFleetId());
        bd.setStatus(baseDriverVO.getStatus());
        bd.setComment(baseDriverVO.getComment());
        bd.setFlag("0");
        baseDriverService.create(bd);
    }

    @Override
    //删除
    public void delBaseDriver(String id) {
        BaseDriver bd=baseDriverService.view(id);
        bd.setFlag("1");
        baseDriverService.update(bd);
    }

    //恢复驾驶员
    @Override
    public void hfBaseDriver(String id) {
        BaseDriver bd=baseDriverService.view(id);
        bd.setFlag("0");
        baseDriverService.update(bd);
    }

    @Override
    //修改
    public void editBaseDriver(BaseDriverVO baseDriverVO) {
        BaseDriverVO qryVo=new BaseDriverVO();
        qryVo.setBaseCompany(baseDriverVO.getBaseCompany());
        qryVo.setMobile(baseDriverVO.getMobile());
        Page p=findAllBaseDriver(qryVo, 1, 1);
        if (p.getContent().size()>0&& !((BaseDriver)p.getContent().get(0)).getId().equals(baseDriverVO.getId())){
            if(((BaseDriver)p.getContent().get(0)).getFlag().equals("1"))
                throw new RestClientException("手机号：\""+baseDriverVO.getMobile()+"\"已存在。该驾驶员停用状态，请恢复");
            else  throw new RestClientException("手机号：\""+baseDriverVO.getMobile()+"\"已存在。");
        }
        BaseDriver bd=baseDriverService.view(baseDriverVO.getId());
        BeanUtils.copyProperties(baseDriverVO,bd);
        if(baseDriverVO.getBaseFleetId()!=null&&!baseDriverVO.getBaseFleetId().equals("")){//所属公司
            BaseFleet bf=new BaseFleet();
            bf.setId(baseDriverVO.getBaseFleetId());
            bd.setBaseFleet(bf);
        }
        bd.setFlag("0");
        bd.setPassword(baseDriverVO.getPassword());
        baseDriverService.update(bd);
    }

    @Override
    //单值查询
    public Object findOneBaseDriver(BaseDriverVO baseDriverVO) {
        BaseDriver bd=new BaseDriver();//创建一个BaseDriver对象
        BeanUtils.copyProperties(baseDriverVO,bd);
        BaseDriver bd1=baseDriverService.findOne(bd);

        return bd1;
    }

    @Override
    //分页查询
    public Page<BaseDriverVO> findAllBaseDriver(BaseDriverVO baseDriverVO, int size, int page) {
        PageRequest pr = new PageRequest();
        List<Term> tr = new ArrayList<>();
        if(baseDriverVO.getFlag()!=null&&!baseDriverVO.getFlag().equals("")){
            tr.add(new Term("flag", baseDriverVO.getFlag()));//删除标记
        }
        if (baseDriverVO.getId() != null && (!baseDriverVO.getId().equals(""))) {
            tr.add(new Term("id", baseDriverVO.getId()));//ID
        }
        if (baseDriverVO.getName() != null && (!baseDriverVO.getName().equals(""))) {
            tr.add(new Term("name", Operator.LK, "%" + baseDriverVO.getName() + "%"));//姓名 模糊查询
        }
        if (baseDriverVO.getBaseFleetId() != null && (!baseDriverVO.getBaseFleetId().equals(""))) {
            tr.add(new Term("baseFleetId", baseDriverVO.getBaseFleetId()));
        }
        if (baseDriverVO.getBaseCompany() != null && (!baseDriverVO.getBaseCompany().equals(""))){//id
            tr.add(new Term("baseCompanyId", baseDriverVO.getBaseCompany()));
        }
        if(baseDriverVO.getIdentityCard()!=null&&!baseDriverVO.getIdentityCard().equals("")){//身份证不为空
            tr.add(new Term("identityCard", baseDriverVO.getIdentityCard()));
        }
        if(baseDriverVO.getDrivingLicence()!=null&&!baseDriverVO.getDrivingLicence().equals("")){//驾驶证不为空
            tr.add(new Term("drivingLicence", baseDriverVO.getDrivingLicence()));
        }
        if(baseDriverVO.getMobile()!=null&&!baseDriverVO.getMobile().equals(""))
        {
            tr.add(new Term("mobile", baseDriverVO.getMobile()));
        }
        Filter filter=new Filter(new Criteria(tr));
            pr.setSize(size);//每页的行数
            pr.setPage(page);//页码
            pr.setFilter(filter);
        Page<BaseDriver> bd1=baseDriverService.findAll(pr);
        Page<BaseDriverVO> bdv1=new Page<>();
        BeanUtils.copyProperties(bd1, bdv1);
        return bdv1;
    }

    @Override
    public void relieveWX(String id) {
        BaseDriver bd = baseDriverService.view(id);//
        bd.setOpenId(null);
        bd.setFlag("0");//flag初始为0
        baseDriverService.update(bd);
    }

    @Override
    public List baseDriverList(String baseCompanyId) {
        BaseDriver baseDriver = new BaseDriver();
        BaseCompany baseCompany = new BaseCompany();
        baseCompany.setId(baseCompanyId);
        baseDriver.setBaseCompany(baseCompany);
        baseDriver.setFlag("0");
        List list = baseDriverService.list(baseDriver,null);
        return list;
    }

    @Override
    public List baseDriverListByFleet(String fleetId) {
        BaseDriver baseDriver = new BaseDriver();
        BaseFleet baseFleet=new BaseFleet();
        baseFleet.setId(fleetId);
        baseDriver.setBaseFleet(baseFleet);
        baseDriver.setFlag("0");
        List list = baseDriverService.list(baseDriver,null);
        return list;
    }
}
